package com.pinyoumei.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.adapter.SignInAdapter;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.bean.SignDataBean;
import com.pinyoumei.app.bean.SignInitBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.DateTimeUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-12.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-12        Administrator             新增Person类
 * 2021-08-12        Administrator             增加sex属性
 * 签到
 */
public class SignInActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvSign;
    private TextView tvSignAll;
    private ImageView ivSignIn;
    private int days;
    private int today;
    private int weekday;
    private SignInAdapter signInAdapter;
    private ArrayList<SignDataBean> list;
    private boolean is_false = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

        InitWidget();

        InitData();
    }

    private void InitWidget() {



        ImageView ivClose = findViewById(R.id.iv_close);
        ivClose.setOnClickListener(this);
        TextView tvRecord = findViewById(R.id.tv_record);
        tvRecord.setOnClickListener(this);
        ivSignIn = findViewById(R.id.iv_sign_in);
        ivSignIn.setOnClickListener(this);


        tvSign = findViewById(R.id.tv_sign);
        tvSignAll = findViewById(R.id.tv_sign_all);


        list = new ArrayList<>();






        //计算当月有多少天
        days = DateTimeUtils.getDays(DateTimeUtils.getYear(), DateTimeUtils.getMonth());
        //一号星期几
        weekday = DateTimeUtils.getWeekdayOfMonth(DateTimeUtils.getYear(), DateTimeUtils.getMonth(), 1);
        //今日
        today = DateTimeUtils.getDate();


        for (int i = 0; i < weekday; i++) {
            SignDataBean signDataBean = new SignDataBean();
            signDataBean.setSign_day_str("");
            list.add(signDataBean);
        }



        RecyclerView rv1 = findViewById(R.id.rv_1);
        rv1.setLayoutManager(new GridLayoutManager(this, 7, GridLayoutManager.VERTICAL, false));
        signInAdapter = new SignInAdapter(R.layout.item_sign, null);
        signInAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        rv1.setAdapter(signInAdapter);
    }

    private void InitData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(SignInActivity.this));


        OkGo.<String>post(Constants.SIGNCENTER)
                .params("user_id", SPUtils.getAppUserId(SignInActivity.this))
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        SignInitBean signBean = gson.fromJson(response.body(), SignInitBean.class);

                        if (signBean.getCode() >= 0) {
                            tvSign.setText(signBean.getResult().getContinue_sign_day()+ "次");
                            tvSignAll.setText(signBean.getResult().getTotal_sign_day()+ "次");

                            //当日是否已签到 0否 1是
                            switch (signBean.getResult().getIs_today_signed()) {
                            	case 0:
                                    ivSignIn.setImageResource(R.mipmap.ic_sing_in_bg3);
                            		break;
                                case 1:
                                    ivSignIn.setImageResource(R.mipmap.ic_sing_in_bg4);
                                    break;
                            	default:
                            		break;
                            }

                            if (signBean.getResult().getSign_day_str()!=null){
                                String[] tags = signBean.getResult().getSign_day_str().split(",");

                                for (int i = 1; i < days+1; i++) {
                                    SignDataBean signDataBean = new SignDataBean();
                                    if (i<=9){
                                        signDataBean.setSign_day_str("0"+i);
                                    }else {
                                        signDataBean.setSign_day_str(""+i);
                                    }
                                    if (i == today){
                                        signDataBean.setDate_bg(true);
                                    }


                                    if (i<today){
                                        signDataBean.setAfter(true);
                                    }

                                    if (tags!=null &&  tags.length>0){
                                        for (int i1 = 0; i1 < tags.length; i1++) {
                                            if (!TextUtils.isEmpty(tags[i1])){
                                                int day = Integer.parseInt(tags[i1]);
                                                if ( day == i){
                                                    signDataBean.setSign(true);
                                                    signDataBean.setDate_bg(false);
                                                }
                                                if (day == today){
                                                    is_false  = true;
                                                }

                                            }

                                        }
                                    }

                                    list.add(signDataBean);
                                }


                            }

                            signInAdapter.setNewData(list);

                        } else {
                            Toast.makeText(SignInActivity.this, signBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }



    private void Sign_In() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(SignInActivity.this));


        OkGo.<String>post(Constants.SIGN_NOW)
                .params("user_id", SPUtils.getAppUserId(SignInActivity.this))
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        BaseBean signBean = gson.fromJson(response.body(), BaseBean.class);

                        if (signBean.getCode() >= 0) {
                            ivSignIn.setImageResource(R.mipmap.ic_sing_in_bg4);
                            for (int i = 0; i < signInAdapter.getData().size(); i++) {
                                if (  signInAdapter.getData().get(i).getSign_day_str().equals(today+"")){
                                    signInAdapter.getData().get(i).setSign(true);
                                    signInAdapter.getData().get(i).setDate_bg(false);
                                }
                            }
                            is_false  = true;
                            signInAdapter.setNewData(signInAdapter.getData());
                            Toast.makeText(SignInActivity.this, signBean.getMsg(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SignInActivity.this, signBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.iv_close:
        		finish();
        		break;
            case R.id.tv_record:
                Intent intent = new Intent(SignInActivity.this,SignRecordActivity.class);
                startActivity(intent);
                break;


                case R.id.iv_sign_in:
                    if (is_false == false){
                        Sign_In();
                    }

                break;
        	default:
        		break;
        }
    }
}
